package cn.edu.sgu.www.mhxysy.service.school.impl;

import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.consts.CustomDirectories;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.school.SchoolMasterDTO;
import cn.edu.sgu.www.mhxysy.entity.school.SchoolMaster;
import cn.edu.sgu.www.mhxysy.mapper.school.SchoolMasterMapper;
import cn.edu.sgu.www.mhxysy.pager.school.SchoolMasterPager;
import cn.edu.sgu.www.mhxysy.service.school.SchoolMasterService;
import cn.edu.sgu.www.mhxysy.util.ImageUpdater;
import cn.edu.sgu.www.mhxysy.util.ObjectUtils;
import cn.edu.sgu.www.mhxysy.util.UploadUtils;
import cn.edu.sgu.www.mhxysy.util.WrapperUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class SchoolMasterServiceImpl implements SchoolMasterService {

    private final UploadUtils uploadUtils;
    private final ImageUpdater imageUpdater;
    private final SchoolMasterMapper schoolMasterMapper;

    @Autowired
    public SchoolMasterServiceImpl(
            UploadUtils uploadUtils,
            ImageUpdater imageUpdater,
            SchoolMasterMapper schoolMasterMapper) {
        this.uploadUtils = uploadUtils;
        this.imageUpdater = imageUpdater;
        this.schoolMasterMapper = schoolMasterMapper;
    }

    @Override
    public String upload(MultipartFile file) throws IOException {
        String directory = CustomDirectories.DIRECTORY_MPSF;

        return uploadUtils.upload(file, directory);
    }

    @Override
    public void insert(SchoolMasterDTO schoolMasterDTO) {
        SchoolMaster schoolMaster = toEntity(schoolMasterDTO);

        schoolMaster.setId(null);

        schoolMasterMapper.insert(schoolMaster);
    }

    @Override
    public void updateById(SchoolMasterDTO schoolMasterDTO) {
        ObjectUtils.requireNonNull(schoolMasterDTO.getId(), CustomMessages.ID_NOT_NULL);

        SchoolMaster schoolMaster = toEntity(schoolMasterDTO);

        schoolMasterMapper.updateById(schoolMaster);
    }

    @Override
    public void updateImageById(Integer id, String image) {
        imageUpdater.updateImageById(image, new Consumer<Object>() {
            @Override
            public void accept(Object o) {
                UpdateWrapper<SchoolMaster> updateWrapper = WrapperUtils.getUpdateWrapper(id, image);

                schoolMasterMapper.update(updateWrapper.getEntity(), updateWrapper);
            }
        });
    }

    @Override
    public List<SchoolMaster> selectAll() {
        return schoolMasterMapper.selectList(null);
    }

    @Override
    public String selectNameBySchoolId(Integer schoolId) {
        QueryWrapper<SchoolMaster> queryWrapper = new QueryWrapper<>();

        queryWrapper.select("name");
        queryWrapper.eq("school_id", schoolId);

        List<SchoolMaster> list = schoolMasterMapper.selectList(queryWrapper);

        if (list.size() == 1) {
            SchoolMaster schoolMaster = list.get(0);

            return schoolMaster.getName();
        }

        StringBuilder name = new StringBuilder();

        for (SchoolMaster schoolMaster : list) {
            name.append(schoolMaster.getName());
            name.append("、");
        }

        // 删除最后一个顿号、
        name.deleteCharAt(name.length() - 1);

        return name.toString();
    }

    @Override
    public Page<SchoolMaster> selectByPage(SchoolMasterPager pager) {
        QueryWrapper<SchoolMaster> queryWrapper = new QueryWrapper<>();
        Page<SchoolMaster> page = Pager.ofPage(pager);

        queryWrapper.eq(
                pager.getSchoolId() != null,
                "school_id", pager.getSchoolId()
        );

        return schoolMasterMapper.selectPage(page, queryWrapper);
    }

    private SchoolMaster toEntity(SchoolMasterDTO schoolMasterDTO) {
        SchoolMaster schoolMaster = new SchoolMaster();

        schoolMaster.setId(schoolMasterDTO.getId());
        schoolMaster.setName(schoolMasterDTO.getName());
        schoolMaster.setSchoolId(schoolMasterDTO.getSchoolId());

        return schoolMaster;
    }

}